package com.shanlitech.et;

import com.google.protobuf.DynamicMessage;
import com.shanlitech.et.pb.Agpub;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;

import java.util.UUID;
@Configuration
@SpringBootApplication
@ServletComponentScan
public class Bootstrap {
public static ZMQ.Socket pub;
    public static void main1(String[] args) throws Exception{
        long start = System.currentTimeMillis();
        Integer tcount = 5;
        final Integer num;
        if(args != null && args.length == 2) {
            if(args[0] != null && !"".equals(args[0])) tcount = Integer.parseInt(args[0]);
            if(args[1] != null && !"".equals(args[1])) {
                num = Integer.parseInt(args[1]);
            } else {
                num = 2;
            }
        } else {
            num = 2;
        }
        ZContext ctx = new ZContext();

        pub = ctx.createSocket(ZMQ.PUB);
        final SpeechRecorder recorder = SpeechRecorder.getInstance();
        pub.bind("tcp://*:6041");

        //final CountDownLatch countDownLatch = new CountDownLatch(tcount * num);
        /*ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=0;i<tcount;i++) {
            executorService.submit(new Runnable() {
                public void run() {

                }
            });
        }
        //countDownLatch.await();
        executorService.shutdown();
        while(true){
            if(executorService.isTerminated()){
                System.out.println("所有线程执行完毕");
                break;
            }
            Thread.sleep(1000);
        }*/
        for(int j=0;j<num;j++) {
            Long sid = 6612403000000000000l + uuid();
            recorder.zpb.send(pub, onMemberGotMic(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onMemberLostMic(sid));

            System.out.println("pub msg " + sid);
            //countDownLatch.countDown();
            //System.out.println(countDownLatch.getCount());
        }
        long end = System.currentTimeMillis();
        System.out.println(tcount+"个线程执行"+num+"次, 耗时"+(end-start)+"ms");
    }

    public static void main(String[] args) {
        SpringApplication.run(Bootstrap.class, args);
        ZContext ctx = new ZContext();
        pub = ctx.createSocket(ZMQ.PUB);
        SpeechRecorder.getInstance();
        pub.bind("tcp://*:6041");
        System.out.println(pub.getSndHWM());
        System.out.println(pub.getRcvHWM());
        pub.setRcvHWM(Integer.MAX_VALUE);
        pub.setSndHWM(Integer.MAX_VALUE);
    }

    static DynamicMessage onMemberGotMic(Long speech_id) {
        /*fd_gotmic_ag_id;
        fd_gotmic_speech_id;
        fd_gotmic_gid;
        fd_gotmic_uid;
        fd_gotmic_payload;*/
        DynamicMessage.Builder builder = DynamicMessage.getDefaultInstance(Agpub.MemberGotMic.getDescriptor()).newBuilderForType();
        SpeechRecorder recorder = SpeechRecorder.getInstance();
        builder.setField(recorder.fd_gotmic_ag_id, "ag-1-1");
        builder.setField(recorder.fd_gotmic_speech_id, speech_id);
        builder.setField(recorder.fd_gotmic_gid, 6600039741105635330l);
        builder.setField(recorder.fd_gotmic_uid, 33);
        builder.setField(recorder.fd_gotmic_payload, 103);
        return builder.build();
    }

    static DynamicMessage onAudioPacket(Long speech_id) {
        /*fd_ap_ag_id;
        fd_ap_speech_id;
        fd_ap_gid;
        fd_ap_uid;
        fd_ap_payload;
        fd_ap_seq;
        fd_ap_ts;
        fd_ap_data;*/
        DynamicMessage.Builder builder = DynamicMessage.getDefaultInstance(Agpub.AudioPacket.getDescriptor()).newBuilderForType();
        SpeechRecorder recorder = SpeechRecorder.getInstance();
        builder.setField(recorder.fd_ap_ag_id, "ag-1-1");
        builder.setField(recorder.fd_ap_speech_id, speech_id);
        builder.setField(recorder.fd_ap_gid, 6600039741105635330l);
        builder.setField(recorder.fd_ap_uid, 33);
        builder.setField(recorder.fd_ap_payload, 103);
        builder.setField(recorder.fd_ap_data, new byte[]{(byte)0x64, (byte)0x33, (byte)0xdb, (byte)0xaa, (byte)0xe2, (byte)0x3d,
        0x37, 0x55, (byte)0xc1, 0x7a, (byte)0x93, 0x6e, 0x10, (byte)0xbd, 0x20, 0x54, 0x37, (byte)0xd3, (byte)0xa3, (byte)0xd2, 0x12, (byte)0x80});
        return builder.build();
    }

    static DynamicMessage onMemberLostMic(Long speech_id) {
        /*fd_lostmic_ag_id;
        fd_lostmic_speech_id;
        fd_lostmic_gid;
        fd_lostmic_uid;
        fd_lostmic_payload;*/
        DynamicMessage.Builder builder = DynamicMessage.getDefaultInstance(Agpub.MemberLostMic.getDescriptor()).newBuilderForType();
        SpeechRecorder recorder = SpeechRecorder.getInstance();
        builder.setField(recorder.fd_lostmic_ag_id, "ag-1-1");
        builder.setField(recorder.fd_lostmic_speech_id, speech_id);
        builder.setField(recorder.fd_lostmic_gid, 6600039741105635330l);
        builder.setField(recorder.fd_lostmic_uid, 33);
        builder.setField(recorder.fd_lostmic_payload, 103);
        return builder.build();
    }

    public static Long uuid() {
        int i = UUID.randomUUID().hashCode();
        if(i < 0) {
            i = -i;
        }
        return new Integer(i).longValue();
    }
}
